package cn.edu.hyit.hjq.volunteems.backend.service.impl;

import cn.edu.hyit.hjq.volunteems.backend.dto.request.ReviewRequest;
import cn.edu.hyit.hjq.volunteems.backend.mapper.RegistrationMapper;
import cn.edu.hyit.hjq.volunteems.backend.mapper.ReviewMapper;
import cn.edu.hyit.hjq.volunteems.backend.dto.request.RegistrationRequest;
import cn.edu.hyit.hjq.volunteems.backend.service.ReviewService;
import org.springframework.stereotype.Service;

@Service
public class ReviewServiceImpl implements ReviewService {

    private final ReviewMapper reviewMapper;
    private final RegistrationMapper registrationMapper;

    public ReviewServiceImpl(ReviewMapper reviewMapper,
                             RegistrationMapper registrationMapper) {
        this.reviewMapper = reviewMapper;
        this.registrationMapper = registrationMapper;
    }

    @Override
    public void audit(ReviewRequest request) {
        reviewMapper.auditReview(request.getRegId(), request.getStatus(), request.getReviewerId());

        if (request.getStatus() == 1) {
            Long regId = request.getRegId();
            RegistrationRequest reg = registrationMapper.findById(regId);
            if (reg == null) {
                // 记录日志或抛出异常
                throw new RuntimeException("报名记录未找到，regId: " + regId);
            }
            String qrCode = "QR-" + reg.getActivityId() + "-" + reg.getVolunteerId() + "-" + System.currentTimeMillis();
            registrationMapper.updateQrCode(regId, qrCode);
        }
    }
}
